﻿//-----------------------------------------------------------------------
// <copyright file="CellSets.cs" company="cx studio">
//     Copyright (c) cx studio. All rights reserved.
// </copyright>
// <author>Xi Chen</author>
//-----------------------------------------------------------------------
namespace CentralFoveaLib.Brain
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class CellSet
    {
        public List<List<Cell>> AixCells { get; set; }
        public IDictionary<int, List<Cell>> MindCells { get; set; }

        public void RecursiveDescent()
        {
            //0->
            //32=2^5
            //那就是递归下降5次
        }
        public void Learning(int level)
        {
           /* var cells = MindCells[level];
            var aixCells = AixCells[level];
           
            Cell firstEmptyCell = null;
            for (int i = 0; i < aixCells.Count; i++)
            {
                var cellAix = aixCells[i];
                if (cellAix.XAxis == null && cellAix.YAxis==null) continue;
                Cell maxCell = null;
                decimal maxSameCell = 0;
                #region
                for (int j = 0; j < cells.Count; j++)
			    {
			        var cell = cells[j];
                    //如果当前的Cell是空
                    if (cell.XAxis == null && cell.YAxis == null)
                    {
                        firstEmptyCell = cell;
                        continue;
                    }
                    //如果不是空的Cell
                    decimal tempRate = cell.IsSame(cellAix);
                    if (Math.Abs(maxSameCell-tempRate)>8)
                    {
                        if (maxSameCell < tempRate)
                        {
                            maxCell = cell;
                            maxSameCell = tempRate;
                        }
                    }

                }
                #endregion
                //如果当前的Y细胞没有记忆，那么他会记忆当前的输入。
                if (maxCell == null)
                {
                    firstEmptyCell.XAxis = cellAix.XAxis;
                    firstEmptyCell.YAxis = cellAix.YAxis;
                    firstEmptyCell.Output = 1;
                }
                else
                {
                    //如果不是空，说明找到了对应的Y细胞
                    //激活Y细胞
                    maxCell.Output += 1;
                }
            }
            PrintL(level);
            * */
        }

        private void PrintL(int level)
        {
            var cells = MindCells[level];
            int count = 0;
            for (int i = 0; i < cells.Count; i++)
            {
                var cell = cells[i];
                if (cell.XAxis != null)
                {
                    /*Console.WriteLine(cell.Output);
                    PrintLine(cell.XAxis);
                    PrintLine(cell.YAxis);
                    Console.WriteLine("**");
                    PrintM(cell.YAxis, cell.XAxis);
                    Console.WriteLine("----------------");
                    count += cell.Output;*/
                }
            }
            Console.WriteLine("共计:"+count);
        }
        private void PrintLine(int[] a)
        {
            StringBuilder builder = new StringBuilder();
            foreach (int item in a)
            {
                builder.Append(item);
            }
            Console.WriteLine(builder.ToString());
        }

        private void PrintM(int[] row, int[] col)
        {
            //int[][] c = new int[row.Length][];
            for (int i = 0; i < row.Length; i++)
            {
                //c[i] = new int[col.Length];
                int rowItem = row[i];
                for (int j = 0; j < col.Length; j++)
                {
                    int colItem = col[j];
                    if (rowItem > 0 && colItem > 0)
                    {
                        Console.Write(1);
                        row[i]--;
                        col[j]--;
                    }
                    else
                    {
                        Console.Write(0);
                    }
                }
                Console.Write("\n");
            }
        }
    }
}
